﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WealowsHospital;
namespace WealowsHospital
{
    public partial class FrmCreateStaff : Form
    {
        private WellmeadowsEntities staffs;
        private BLL.StaffBus staffBus;
        private Staff currentStaff;
        private List<Experience> currentExperience;
        private int max;
        public FrmCreateStaff()
        {
            InitializeComponent();
            staffs = new WellmeadowsEntities();
            staffBus = new WealowsHospital.BLL.StaffBus(staffs);
            updateButtonStatus(true);
            cbbRole.DataSource = staffs.Role.ToList();
            cbbRole.DisplayMember = "name";
            cbbRole.ValueMember = "id";
            GetMaxStaffID();
            currentExperience = new List<Experience>();
            btnAddStaff.Enabled = false;
        }

        private void chkGeneratePassword_Click(object sender, EventArgs e)
        {
            txtPassword.Text=txtFirstName.Text+txtLastName;
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            GetMaxStaffID();
            currentStaff=staffBus.CreateStaff(
                txtCurrentStaffID.Text,
                txtFirstName.Text,
                txtLastName.Text, 
                txtAddress.Text, 
                cbbGender.Text,
                Convert.ToDateTime(dtDateOfBirth.Text), 
                txtInsuranceNumber.Text,
                txtPhone.Text, 
                Convert.ToDecimal(txtCurrentSalary.Text), 
                Convert.ToInt16(txtWorkPerHours.Text), 
                txtSalaryScale.Text, 
                cbbSalaryPayment.Text,
                txtPassword.Text,
                Convert.ToInt16(cbbRole.SelectedValue));
            if (currentStaff!=null)
            {
                MessageBox.Show("Create Successful");
                updateButtonStatus(false);
                txtCurrentStaffID.Text = "S" + max;
                
            }
            else
            {
                //cap nhat lai id neu bi trung id
                GetMaxStaffID();
                btnAdd_Click(sender, e);
            }
            tabUserInfor.Enabled = true;
            
        }

        private void FrmCreateStaff_FormClosing(object sender, FormClosingEventArgs e)
        {
            staffs.Dispose();
        }

        
        private void btnUpdateStaff_Click(object sender, EventArgs e)
        {
            if (staffBus.UpdateStaff(
                txtCurrentStaffID.Text,
                txtFirstName.Text,
                txtLastName.Text,
                txtAddress.Text,
                cbbGender.Text,
                Convert.ToDateTime(dtDateOfBirth.Text),
                txtInsuranceNumber.Text,
                txtPhone.Text,
                Convert.ToDecimal(txtCurrentSalary.Text),
                Convert.ToInt16(txtWorkPerHours.Text),
                txtSalaryScale.Text,
                cbbSalaryPayment.Text,
                txtPassword.Text,
                Convert.ToInt16(cbbRole.SelectedValue)))
            {
                MessageBox.Show("Update Staff's Background Information Successful");
            }
            else
            {
                MessageBox.Show("Cannot Update This Staff's Background Information");
            }
        }
        //kiem soat cac nut bam cua staff
        private void updateButtonStatus(bool status)
        {
            btnAddStaff.Enabled = status;
            btnDeleteStaff.Enabled = btnUpdateStaff.Enabled = !status;
            btnDeleteReal.Enabled = !status;
        }

        private void FrmCreateStaff_Load(object sender, EventArgs e)
        {
            List<Role> roles = staffs.Role.ToList();
            cbbRole.DataSource = roles;
            cbbRole.DisplayMember = "name";
            cbbRole.ValueMember = "id";
        }

        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }
        public void GetMaxStaffID()
        {
            List<string> strStaff=staffs.Staff.Select(st => st.StaffID).ToList();
            max = 0;
            foreach(string s in strStaff)
            {
                //lay so lon nhat de cap nhat de lam id tiep theo
                int val = Convert.ToInt16(s.Substring(1, s.Length-1));
                if (val >= max)
                    max = val;
            }
            max++;
            txtCurrentStaffID.Text = "S" + max;
        }

        //them qualification vao db
        private void btnAddQua_Click(object sender, EventArgs e)
        {
            if (staffBus.AddQualification(currentStaff, Convert.ToDateTime(dateGraduation.Text), txtInstitution.Text, txtTypeOfEdu.Text))
            {
                MessageBox.Show("Add Qualification Successful");
                UpdateSourceQualification();
            }
            else
            {
                MessageBox.Show("Add Qualification Fail");
            }
            btnAddQua.Visible = false;

        }

      
    
        //search staff by ID
        private void btnSearch_Click(object sender, EventArgs e)
        {
            currentStaff = staffs.Staff.Where(st => st.StaffID == txtSearchStaffID.Text).FirstOrDefault();
            if (currentStaff != null)
            {
                UpdateSourceRole();
                UpdateSourceExperience();
                UpdateSourceQualification();
                tabUserInfor.Enabled = true;
            }
            else
            {
                MessageBox.Show("Not Found");
            }
        }
        /// <summary>
        /// cap nhat lai bindsource cho role
        /// </summary>
        private void UpdateSourceRole()
        {
            if (currentStaff != null)
            {
                bindSourceStaff.DataSource = currentStaff;
                bindSourceRole.DataSource = currentStaff.Role;
                updateButtonStatus(false);
            }
        }
        /// <summary>
        /// update source cho qualification
        /// </summary>
        private void UpdateSourceQualification()
        {
            if (currentStaff != null)
            {
                currentStaff.Qualifications.Load();
                bindSourceQualification.DataSource = currentStaff.Qualifications.ToList();
            }
        }

        /// <summary>
        /// update experience source
        /// </summary>
        private void UpdateSourceExperience()
        {
            if (currentStaff != null)
            {
                currentStaff.Experience.Load();
                currentExperience = currentStaff.Experience.ToList();
                bindSourceExperience.DataSource = currentExperience;
            }
        }
        //them nut save qualification
        private void bindingNavigatorAddNewItem_Click(object sender, EventArgs e)
        {
            btnAddQua.Visible = true;
        }


        //luu thong tin experience
        private void btnSaveExperience_Click(object sender, EventArgs e)
        {

            Experience experience = staffBus.AddExperience(currentStaff,
                                    Convert.ToDateTime(dateStartWork.Text),
                                    Convert.ToDateTime(dateEndWork.Text),
                                    txtOrganization.Text);
            if (experience != null)
            {
                MessageBox.Show("Add Experience Successful");
                UpdateSourceExperience();
                
            }
            else
            {
                MessageBox.Show("Add Experience Fail");
            }
            btnSaveExperience.Visible = false;
        }


        //xoa qualification
        private void bindingNavigatorDeleteItem_Click_1(object sender, EventArgs e)
        {
            
            if (staffBus.DeleteQualification(Convert.ToInt32(txtQuadId.Text)))
            {
                MessageBox.Show("Deleted Qualification");
                UpdateSourceQualification();
            }
            else
            {
                MessageBox.Show("Cannot Delete This Qualification");
            }
        }

        //xoa experience
        private void btnDeleteExper_Click(object sender, EventArgs e)
        {
            if (staffBus.DeleteExperience(Convert.ToInt32(txtExperienceID.Text)))
            {
                MessageBox.Show("Deleted Experience");
                UpdateSourceExperience();
            }
            else
            {
                MessageBox.Show("Cannot Delete Experience");
            }
        }

        private void bindingNavigatorAddNewItem1_Click_1(object sender, EventArgs e)
        {
            btnSaveExperience.Visible = true;
        }

        private void btnUpadateQuad_Click(object sender, EventArgs e)
        {
            if (staffBus.UpdateQualification(Convert.ToInt32(txtQuadId.Text),
                Convert.ToDateTime(dateGraduation.Text),
                txtInstitution.Text,
                txtTypeOfEdu.Text))
            {
                MessageBox.Show("Updated Qualification");
                UpdateSourceQualification();
            }
            else
            {
                MessageBox.Show("Cannot Update Qualification");
            }
        }

        private void btnUpadeExper_Click(object sender, EventArgs e)
        {
            if (staffBus.UpdateExperience(Convert.ToInt32(txtExperienceID.Text),
                Convert.ToDateTime(dateStartWork.Text),
                Convert.ToDateTime(dateEndWork.Text),
                txtOrganization.Text))
            {
                MessageBox.Show("Update Experience Success");
                UpdateSourceQualification();
            }
            else
            {
                MessageBox.Show("Cannot Update Experience");
            }
        }

        private void btnDeleteStaff_Click(object sender, EventArgs e)
        {
            if (staffBus.TmpDeleteStaff(txtCurrentStaffID.Text))
            {
                MessageBox.Show("Delete This Staff");
                tabUserInfor.Enabled = false;
            }
            else
            {
                MessageBox.Show("Cannot Delete This Staff");
            }
            
        }

        private void btnDeleteReal_Click(object sender, EventArgs e)
        {
            if (staffBus.DeleteStaff(txtCurrentStaffID.Text))
            {
                MessageBox.Show("Absolute Delete This Staff");
                tabUserInfor.Enabled = false;
            }
            else
            {
                MessageBox.Show("Cannot Delete This Staff");
            }
        }

        private void btnAddData_Click(object sender, EventArgs e)
        {
            updateButtonStatus(true);
            GetMaxStaffID();
        }

       
    }
}
